package com.dozen.commonbase.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.dozen.commonbase.R;

/**
 * @author: Dozen
 * @description:
 * @time: 2021/8/12
 */
public class StarMoreView extends LinearLayout {

    private Context mContext;
    private LinearLayout mContentView;
    private int totalStar = 5;
    private int selectStar = 0;
    private int marginEnd = 0;
    private int starPictureSelect = R.mipmap.star_green;
    private int starPictureUnSelect = R.mipmap.star_gray;
    private int starPictureHalf = R.mipmap.star_green_half;
    private boolean isChange = true;
    private boolean isExistHalf = false;
    private boolean showHalf = false;

    public StarMoreView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView(context, attrs);
    }

    public StarMoreView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.StarMoreView);

        totalStar = ta.getInteger(R.styleable.StarMoreView_star_total_number, totalStar);
        selectStar = ta.getInteger(R.styleable.StarMoreView_star_now_number, selectStar);
        marginEnd = ta.getInteger(R.styleable.StarMoreView_star_margin_end, marginEnd);
        starPictureSelect = ta.getResourceId(R.styleable.StarMoreView_star_picture_select, starPictureSelect);
        starPictureHalf = ta.getResourceId(R.styleable.StarMoreView_star_picture_half, starPictureHalf);
        starPictureUnSelect = ta.getResourceId(R.styleable.StarMoreView_star_picture_un_select, starPictureUnSelect);
        isChange = ta.getBoolean(R.styleable.StarMoreView_star_can_change, isChange);
        isExistHalf = ta.getBoolean(R.styleable.StarMoreView_star_exist_half, isExistHalf);
        showHalf = ta.getBoolean(R.styleable.StarMoreView_star_show_half, showHalf);

        ta.recycle();

        setOrientation(HORIZONTAL);

        mContentView = new LinearLayout(mContext);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mContentView.setOrientation(HORIZONTAL);
        mContentView.setLayoutParams(lp);
        addView(mContentView, 0);

        addStar(totalStar, selectStar);
        setSelectStar(selectStar);
    }

    public void openExistHalf(boolean isExistHalf) {
        this.isExistHalf = isExistHalf;
    }

    public void showHalf(boolean showHalf) {
        this.showHalf = showHalf;
        setSelectStar(selectStar);
    }

    public boolean isShowHalf() {
        if (!isExistHalf) {
            return false;
        }
        return showHalf;
    }

    public int getTotalStar() {
        return totalStar;
    }

    public void setTotalStar(int totalStar, int selectStar) {
        this.totalStar = totalStar;
        this.selectStar = selectStar;
        mContentView.removeAllViews();
        addStar(totalStar, selectStar);
    }

    public int getSelectStar() {
        return selectStar;
    }

    public void setSelectStar(int selectStar) {
        this.selectStar = selectStar;
        for (int i = 0; i < totalStar; i++) {
            FrameLayout star = (FrameLayout) mContentView.getChildAt(i);
            star.getChildAt(0).setVisibility(VISIBLE);
            star.getChildAt(1).setVisibility(GONE);
            star.getChildAt(2).setVisibility(GONE);
        }
        for (int i = 0; i < selectStar; i++) {
            if (i >= totalStar) {
                return;
            }
            FrameLayout star = (FrameLayout) mContentView.getChildAt(i);
            star.getChildAt(0).setVisibility(GONE);
            if (isExistHalf && showHalf && i == selectStar - 1) {
                star.getChildAt(2).setVisibility(VISIBLE);
            } else {
                star.getChildAt(1).setVisibility(VISIBLE);
            }

        }
    }

    private void addStar(int total, int selectStar) {
        for (int i = 0; i < total; i++) {
            addStarView(i, selectStar > i);
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    private void addStarView(int index, boolean show) {
        FrameLayout coverView = new FrameLayout(mContext);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.CENTER;
        coverView.setLayoutParams(lp);

        coverView.addView(addFullBgView(!show), 0);
        coverView.addView(addFullView(show), 1);
        coverView.addView(addHalfView(false), 2);

        coverView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (isChange) {
                    if (isExistHalf) {
                        showHalf = !showHalf;
                    }
                    setSelectStar(index + 1);
                }
                return false;
            }
        });

        mContentView.addView(coverView, index);
    }

    private ImageView addFullBgView(boolean show) {
        ImageView imageView = new ImageView(mContext);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.CENTER;
        lp.setMargins(0, 0, marginEnd, 0);
        imageView.setLayoutParams(lp);
        imageView.setBackgroundResource(starPictureUnSelect);
        imageView.setVisibility(show ? VISIBLE : INVISIBLE);
        return imageView;
    }

    private ImageView addFullView(boolean show) {
        ImageView imageView = new ImageView(mContext);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.CENTER;
        lp.setMargins(0, 0, marginEnd, 0);
        imageView.setLayoutParams(lp);
        imageView.setBackgroundResource(starPictureSelect);
        imageView.setVisibility(show ? VISIBLE : INVISIBLE);
        return imageView;
    }

    private ImageView addHalfView(boolean show) {
        ImageView imageView = new ImageView(mContext);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.CENTER;
        lp.setMargins(0, 0, marginEnd, 0);
        imageView.setLayoutParams(lp);
        imageView.setBackgroundResource(starPictureHalf);
        imageView.setVisibility(show ? VISIBLE : INVISIBLE);
        return imageView;
    }

}
